/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.Akun;
import entity.KelolaAkun;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Aditya-Z
 */
public class LogikaAkun {

    HttpServletRequest request;

    public LogikaAkun(HttpServletRequest request) {
        this.request = request;
    }

    public String daftar() {
        HttpSession session = request.getSession();
        Akun akun = new Akun();
        KelolaAkun ka = new KelolaAkun();
        
        String username = request.getParameter("username");
        String umur = request.getParameter("umur");
        String berdan = request.getParameter("berdan");
        String goldar = request.getParameter("goldar");
        String pass = request.getParameter("pass");
        String pass1 = request.getParameter("pass1");
        
        if (username.equals("") ||  umur.equals("")|| berdan.equals("")||goldar.equals("") || pass.equals("") || pass1.equals("")) {
            return "RegisterError.jsp";
        } else {

        akun.setUsername(username);
        akun.setUmur(umur);
        akun.setBeratbadan(berdan);
        akun.setGolongandarah(goldar);
        akun.setPassword(pass);
        akun.setUlangiPassword(pass1);
        // session.setAttribute("tempReg", akun);
        
        if (username.equals("admin")){
            return "dashboard.jsp";
        }

        if (username == null || umur == null || berdan == null
                || goldar == null || pass == null || pass1 == null) {
            return "errordaftar.jsp";
        } else {
            try {
                int Umur = Integer.parseInt(umur);
                int Berda = Integer.parseInt(berdan);
            } catch (NumberFormatException nfe) {
                return "errorint.jsp";
            }

            boolean hasilCheck = ka.checkAkun(username);
            if (!hasilCheck) {
                ka.createAkun(akun);
                return "daftarsukses.jsp";
            } else {
                return "errorusername.jsp";
            }      




        }
    }}

    public String login() {
        HttpSession session = request.getSession();
        Akun akun = new Akun();
        KelolaAkun ka = new KelolaAkun();
        String username = request.getParameter("username");
        String password = request.getParameter("pass");

        akun = ka.getAkun(username);
        if (username.equals(akun.getUsername()) && password.equals(akun.getPassword())) {
            session.setAttribute("uname", username);
            request.setAttribute("akun", akun);
            return "Home.jsp";
        } else {
            return "gagalLogin.jsp";
        }
        
    }

    public String logout() {
        HttpSession session = request.getSession();
        session.removeAttribute("uname");
        session.invalidate();
        return "formLogin.jsp";
    }

    public String openHome() {
        HttpSession session = request.getSession();
        Akun akun = new Akun();
        KelolaAkun ka = new KelolaAkun();
        if (session.getAttribute("uname") != null) {
            String username = (String) session.getAttribute("uname");
            akun = ka.getAkun(username);
            request.setAttribute("akun", akun);
            return "Home.jsp";
        } else {
            return "halamanSudahLogout.jsp";
        }

    }

  /*  public String getAllAkun() {
        HttpSession session = request.getSession();
        List akun = null;
        KelolaAkun ka = new KelolaAkun();
        if (session.getAttribute("uname") != null) {
            String username = (String) session.getAttribute("uname");
            akun = ka.getAllAkun();
            request.setAttribute("akun", akun);
            return "tes.jsp";
        } else {
            return "halamanSudahLogout.jsp";
        }*/

    }
   

